﻿#include "ndt_base_view.h"
#include "ruler_manager.h"

namespace eintik::presentation::viewcore {

NdtScanView::NdtScanView(QWidget *parent)
    : QGraphicsView(parent)
{
    // 1. 先完成视图自身的基础配置（如窗口属性、上下文菜单等）
    setWindowFlags(Qt::Window);
    setContextMenuPolicy(Qt::CustomContextMenu);

    // 2. 初始化 RulerManager，传递当前视图作为父对象
    rulerManager = new RulerManager(this);

    // 3. 调用 RulerManager 的初始化方法，创建标尺实例
    rulerManager->initRulers();

    // 4. 后续初始化（如信号槽连接、视图配置等）
    initView();
    initSlot();
}

void NdtScanView::initView()
{
    fooWidget = new QWidget(this);
    fooWidget->setFixedSize(1, 1);

    // QT QGraphicsItem 消除重影 移动重影 全刷新
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}

void NdtScanView::initView(int type)
{
    m_Type = static_cast<VIEW_TYPE>(type);
    rulerManager->initView(m_Type);
}

void NdtScanView::initSlot()
{
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    QObject::connect(this, &QWidget::customContextMenuRequested, this,
                     &NdtScanView::signalMouseLongPress);
}

VIEW_TYPE NdtScanView::getViewType() const
{
    return m_Type;
}

void NdtScanView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);

    // 从 RulerManager 获取计算好的边距
    int left, top, right, bottom;
    rulerManager->calculateMargins(left, top, right, bottom);
    this->setViewportMargins(left, top, right, bottom);

    rulerManager->resizeRulers(this->size().width(), this->size().height(), left, right,
                                          bottom);
    refreshRulerPosition();
}

RulerManager *NdtScanView::getRulerManager()
{
    return rulerManager;
}

void NdtScanView::setColorMap(const QVector<QColor> &colorVector, int32_t id)
{
    if (((id >= 0) && (id < RULER_CNT))) {
        rulerManager->getRuler(id)->setColorMap(colorVector);
    }
}

void NdtScanView::refreshRulerPosition()
{
    rulerManager->refreshRulerPosition(rotateType);
    fooWidget->move(0, this->size().height() - RULER_SIZE + 4);
}

}
